# import time
import time

from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse

from web.forms.project import ProjectModelForm
from web import models
from utils.tencent.cos import cos_create
# from utils.tencent.cos import create_bucket


def project_list(request):

    #获取我创建的所有项目 + 我参加的所有项目 其中都有星标与否的标志
    #project_list 展示三个栏目：
    #1.星标项目：
    #2.我创建的项目：
    #3.我参与的项目：




    # user_obj = request.tracer.user
    # user_name = user_obj.user_name
    # project_obj = request.tracer.price_policy#标准项目数量
    # project_num = project_obj.project_num
    #
    # return  HttpResponse("%s ,您一共可以创建%d个项目！"%(user_name, project_num))
    # return  render(request,'project_list.html')
    if request.method == "GET":
        project_dict = {'star': [], 'my': [], 'join': []}

        my_project_list = models.Project.objects.filter(creator=request.tracer.user)

        my_join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)

        # my_star_project_list = my_project_list.filter(star=True)+ my_join_project_list.filter(star=True)
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'my'})
            else:
                project_dict['my'].append(row)
        for row in my_join_project_list:
            if row.star:
                project_dict['star'].append({'value': row.project, 'type': 'join'})
            else:
                project_dict['join'].append(row.project)
        form = ProjectModelForm(request)
        return render(request, 'project_list.html', {'form': form,'project_dict':project_dict})
    else:
        form = ProjectModelForm(request, data=request.POST)
        if form.is_valid():
            #为项目创建一个桶
            #同的ID  设计
            bucket = "{}-{}-1303881619".format(request.tracer.user.mobile_phone,str(int(time.time())))
            # 创建COS桶
            cos_create(bucket=bucket)#默认region = ap-nanjing

            #将cos 桶的信息写入数据库
            form.instance.bucket = bucket
            form.instance.region = 'ap-nanjing'
            form.instance.creator = request.tracer.user#因为form并没有Project.creator 直接保存不得行
            instance = form.save()

            # print('form is ok')
            #项目初始化问题类型
            issues_type_object_list = []
            for item in models.IssuesType.PROJECT_INIT_LIST:
                issues_type_object_list.append(models.IssuesType(project=instance,title=item))

            models.IssuesType.objects.bulk_create(issues_type_object_list)



            return JsonResponse({'status':True,})

        else:
            # print('form is not ok')
            # print(form.errors)

            return  JsonResponse({'status':False,'errors':form.errors})


def project_star(request, project_type, project_id):
    """ 星标项目 """
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')

    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    """ 取消星标 """
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')

    return HttpResponse('请求错误')
